//
//  WmcEditWatermarkVC+Delegate.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/8/10.
//

import UIKit

// MARK: - 扩展WmcEditWatermarkVC + UITableViewDelegate

extension WmcEditWatermarkVC: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            guard let group = dataList.yp.object(at: indexPath.section) as? [WmcWatermarkOptionModel],
                  let model = group.yp.object(at: indexPath.row) else { return }
            // 询问外部代理
            if let watermarkModel = viewModel.watermarkModel,
               let action = editWatermarkDelegate?.tableViewDidSelectRow(.current,
                                                                         tableView: tableView,
                                                                         indexPath: indexPath,
                                                                         watermarkModel: watermarkModel,
                                                                         option: model)
            {
                action.transactionDefaultCase { [weak self] in // 默认处理规则
                    guard let weakSelf = self else { return }
                    weakSelf.didSelectOption(indexPath, option: model)
                }
            } else {
                didSelectOption(indexPath, option: model)
            }
        } else {
            guard let group = dataList.yp.object(at: indexPath.section) as? [WmcEditWatermarkSettingCell.Model],
                  let model = group.yp.object(at: indexPath.row) else { return }
            switch model.settingType {
            case .alpha:
                didSelectWatermarkAlpha(model) // 水印透明度
            case .size:
                didSelectWatermarkSize(model) // 水印大小
            }
        }
    }
}

// MARK: - 扩展WmcEditWatermarkVC + UITableViewDataSource

extension WmcEditWatermarkVC: UITableViewDataSource {
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == 0 { return WmcEditWatermarkHeaderView() }
        let view = UIView()
        view.backgroundColor = .wmc_grayH21_FAFAFA
        return view
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 { return 44 }
        return 8
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return .YP.Min
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let defaultHeight: CGFloat = 56
        guard let watermarkModel = viewModel.watermarkModel else { return defaultHeight }
        let sectionArr = dataList.yp.object(at: indexPath.section)
        var cellHeight = defaultHeight
        // 询问外部代理
        if indexPath.section == 0 { // 第一组
            if let editList = sectionArr as? [WmcWatermarkOptionModel],
               let option = editList.yp.object(at: indexPath.row),
               let height = editWatermarkDelegate?.tableViewHeightForAttributeRow(.current,
                                                                                  tableView: tableView,
                                                                                  indexPath: indexPath,
                                                                                  watermarkModel: watermarkModel,
                                                                                  fieldType: option.fieldType,
                                                                                  defaultHeight: defaultHeight)
            {
                cellHeight = height
            }
        } else if indexPath.section == 1 { // 第二组
            if let settingList = sectionArr as? [WmcEditWatermarkSettingCell.Model],
               let model = settingList.yp.object(at: indexPath.row),
               let height = editWatermarkDelegate?.tableViewHeightForBasicRow(.current,
                                                                              tableView: tableView,
                                                                              indexPath: indexPath,
                                                                              watermarkModel: watermarkModel,
                                                                              basicItem: model.wmcBasicItem,
                                                                              defaultHeight: defaultHeight)
            {
                cellHeight = height
            }
        }
        return cellHeight
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return dataList.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let arr = dataList.yp.object(at: section) { return arr.count }
        return 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let sectionArr = dataList.yp.object(at: indexPath.section)
        if indexPath.section == 0 {
            if let watermarkModel = viewModel.watermarkModel,
               let editList = sectionArr as? [WmcWatermarkOptionModel],
               let model = editList.yp.object(at: indexPath.row)
            {
                // 询问外部代理是否替换Cell
                if let watermarkModel = viewModel.watermarkModel,
                   let customCell = editWatermarkDelegate?.tableViewReplaceCellForRow(.current,
                                                                                      tableView: tableView,
                                                                                      indexPath: indexPath,
                                                                                      watermarkModel: watermarkModel,
                                                                                      option: model)
                {
                    return customCell
                } else {
                    var cell: WmcEditWatermarkBaseCell = .init()
                    switch model.fieldType {
                    case .brand:
                        let brandCell: WmcEditWatermarkBrandCell = tableView.yp.dequeueReusableCell()
                        cell = brandCell
                    default:
                        let attributeCell: WmcEditWatermarkAttributeCell = tableView.yp.dequeueReusableCell()
                        cell = attributeCell
                    }
                    cell.configModel(model, styleMode: viewModel.watermarkStyleMode, watermarkModel: watermarkModel)
                    cell.indexPath = indexPath
                    cell.switchAnctionBlock = { [weak self] idx, model in
                        guard let weakSelf = self else { return }
                        // 数据源修改
                        weakSelf.changeWatermarkOption(idx.row, option: model)
                    }
                    return cell
                }
            }
            return UITableViewCell()
        } else {
            let cell: WmcEditWatermarkSettingCell = tableView.yp.dequeueReusableCell()
            if let settingList = sectionArr as? [WmcEditWatermarkSettingCell.Model],
               let model = settingList.yp.object(at: indexPath.row)
            {
                cell.configModel(model)
            }
            return cell
        }
    }
}
